/*
 * Lab12C.cpp
 *    CSIS 248
 */


#include <sys/time.h>
#include <sys/resource.h>
struct timeval start_timevalue_user;
struct timeval start_timevalue_system;
void start_clock(){
	struct rusage  resource_usage;
	getrusage( RUSAGE_SELF, &resource_usage );
	start_timevalue_user   = resource_usage.ru_utime;
	start_timevalue_system = resource_usage.ru_stime;
}

#include <stdio.h>
void stop_clock(){
	struct rusage  resource_usage;
	getrusage( RUSAGE_SELF, &resource_usage );
	struct timeval end_timevalue_user   = resource_usage.ru_utime;
	struct timeval end_timevalue_system = resource_usage.ru_stime;

	double end_tu = end_timevalue_user  .tv_sec + end_timevalue_user  .tv_usec / 1.0e6;
	double end_ts = end_timevalue_system.tv_sec + end_timevalue_system.tv_usec / 1.0e6;

	double start_tu = start_timevalue_user  .tv_sec + start_timevalue_user  .tv_usec / 1.0e6;
	double start_ts = start_timevalue_system.tv_sec + start_timevalue_system.tv_usec / 1.0e6;

	printf("   User time:   %.2f\t\tSystem time: %.2f\n", (end_tu - start_tu), (end_ts - start_ts));
}

#include <math.h>
void process(double * a, int n){
   int i=0;
   for (i=0; i<n; i++)
      a[i] = sqrt(i);
}

#include <stdlib.h>

int main(){
   const int MIN_PROBLEM_SIZE = 1024;
   const int MAX_PROBLEM_SIZE = 1024*1024*256;


   long size = 0;
   for (size=MIN_PROBLEM_SIZE; size<=MAX_PROBLEM_SIZE; size=size*2){
	/*
	 * set up random data for sorting
	 */
	double* data = malloc( size * sizeof(double) );

	/*
	 * Start clock, sort, and stop clock.
	 */
	printf("Processing (size=%ld) ... ", size );
	start_clock();

	process( data, size );

	stop_clock();
	free( data );
	printf("... done!\n");
   }
}
